#include <iostream>
#include <vector>

using namespace std;
class Solution
{
public:
    vector<int> replaceElements(vector<int> &arr)
    {
        int n = arr.size();
        if (1 == n)
        {
            return {-1};
        }
        if (2 == n)
        {
            return {arr[1], -1};
        }
        for (int i = 0; i < n - 2; i++)
        {
            arr[i] = *max_element(arr.begin() + i + 1, arr.end());
        }
        arr[n - 2] = arr[n - 1];
        arr[n - 1] = -1;
        return arr;
    }
    vector<int> replaceElements_2(vector<int> &arr)
    {
        vector<int> res(arr.size());
        res[arr.size() - 1] = -1;
        for (int i = arr.size() - 2; i >= 0; i--)
        {
            res[i] = max(arr[i + 1], res[i + 1]);
        }
        return res;
    }
};
int main()
{
    Solution s;
    vector<int> arr = {17, 18, 5, 4, 6, 1};
    vector<int> ans = s.replaceElements(arr);
    for (int a : ans)
    {
        cout << a << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}
